import {
    createRouter,
    createWebHashHistory
} from 'vue-router'
import store from '@/store/index'
const router = createRouter({
    history: createWebHashHistory(), // 路由模式：哈希模式
    routes: [{
            path: '/',
            redirect: { // 路由重定向
                path: '/index'
            },
            component: () => import('@/views/home.vue'),
            children: [{
                    path: '/image',
                    component: () => import('@/views/image/image.vue')
                },
                {
                    path: '/index',
                    component: () => import('@/views/index/index.vue')
                },
                {
                    path: '/shop/goods/list',
                    component: () => import('@/views/shop/goods')
                },
                {
                    path:'/shop/goods/create',
                    component:()=>import('@/views/shop/create')
                },
                {
                    path: '/shop/category/list',
                    component: () => import('@/views/shop/category')
                },
                {
                    path: '/shop/sku/list',
                    component: () => import('@/views/shop/sku')
                },
                {
                    path: '/shop/type/list',
                    component: () => import('@/views/shop/type')
                },
                {
                    path: '/shop/comment/list',
                    component: () => import('@/views/shop/comment')
                },
                {
                    path: '/order/order/list',
                    component: () => import('@/views/order/order')
                },
                {
                    path: '/order/invoice/list',
                    component: () => import('@/views/order/invoice')
                },
                {
                    path: '/user/user-list/list',
                    component: () => import('@/views/user/user-list')
                },
                {
                    path: '/user/user-level/list',
                    component: () => import('@/views/user/user-level')
                },
                {
                    path: '/set/base',
                    component: () => import('@/views/set/base')
                },
                {
                    path: '/set/express',
                    component: () => import('@/views/set/express')
                },
                {
                    path: '/set/manager',
                    component: () => import('@/views/set/manager')
                },
                {
                    path: '/set/payment',
                    component: () => import('@/views/set/payment')
                },
            ]
        },
        {
            path: '/login',
            component: () => import('@/views/login/login.vue')
        }
    ]
})
router.beforeEach((to,from,next)=>{
    if(to.path == '/login') return next()
    let token = store.state.user.token
    if(token){
        next()
    }else{
        alert('请先登录')
        return next('/login')
    }
})
export default router;